# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=gimp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.10.32
pkgrel=5
pkgdesc="GNU Image Manipulation Program (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.gimp.org/"
license=('spdx:GPL-3.0-or-later AND LGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-babl"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-dbus-glib"
         "${MINGW_PACKAGE_PREFIX}-drmingw"
         "${MINGW_PACKAGE_PREFIX}-gegl"
         "${MINGW_PACKAGE_PREFIX}-gexiv2"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-glib-networking"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-iso-codes"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-libmng"
         "${MINGW_PACKAGE_PREFIX}-libmypaint"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libwmf"
         "${MINGW_PACKAGE_PREFIX}-mypaint-brushes"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-python2-pygtk"
         "${MINGW_PACKAGE_PREFIX}-python2-gobject2"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-pygobject2-devel"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-libjxl"
             "intltool")
optdepends=("${MINGW_PACKAGE_PREFIX}-libjxl: for JPEG XL support")
options=('strip' 'makeflags')
source=(https://download.gimp.org/pub/gimp/v${pkgver%.*}/${_realname}-${pkgver}.tar.bz2
        0001-find-directx-fix.patch
        0002-python-m4.patch
        0003-mingw-add-ModuleFileName-dir-to-PATH.patch
        0004-replace-pygtk-codegen.patch
        0005-fix-link-python-plugins.patch
        0007-clang-rc-files-fix.patch)
sha256sums=('3f15c70554af5dcc1b46e6dc68f3d8f0a6cc9fe56b6d78ac08c0fd859ab89a25'
            'f7f9badc547f1a0317b95e012530db8df63b7c7038c9d9ff8d0b510c86f43439'
            'ae9b6e8fa40fd471cb58f17454219abf531565b5fbaf616e5fc81b3869bbc4b5'
            '682656d1f7ec70bfd3dd10eba3f8db8abb75c80e44933f8c09c730cea5aeb549'
            '98b6212e669eb3a93877514ee6d3284cb3a44ae6d57b6ecb29de3a076db3dbd2'
            '01651bb582b2120aae67752f6a28c0750ea690d46259d855e28f8b18da567bf5'
            'f4f8258b352530b0f9dd1ebd4f4730125caa4d047ca17bf9e5180b52be19c117')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
    0001-find-directx-fix.patch \
    0002-python-m4.patch \
    0003-mingw-add-ModuleFileName-dir-to-PATH.patch \
    0004-replace-pygtk-codegen.patch \
    0005-fix-link-python-plugins.patch \
    0007-clang-rc-files-fix.patch

  sed -e "s|-Wl,-rpath '-Wl,\$\$ORIGIN/../lib'||g" -i app/Makefile.am

  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  CFLAGS+=" -Wno-error=int-conversion" CXXFLAGS+=" -Wno-error=int-conversion" \
  PYTHON="${MINGW_PREFIX}/bin/python2" \
  ../${_realname}-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --sysconfdir="${MINGW_PREFIX}"/etc \
    --enable-mp \
    --enable-gimp-console \
    --enable-python \
    --without-aa \
    --with-pdbgen \
    --with-directx-sdk="${MINGW_PREFIX}"/${MINGW_CHOST} \
    --without-x \
    --without-webkit \
    INTLTOOL_PERL="/usr/bin/perl.exe"

  # V=1 because of a race condition (a generated 'bin2c' type
  # file ending up truncated once). This might avoid it.
  make V=1
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  ln -s gimptool-2.0 "${pkgdir}${MINGW_PREFIX}/bin/gimptool.exe"
  sed -e "s|${MINGW_PREFIX}/bin/python2|python2w.exe|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/gimp/2.0/interpreters/pygimp.interp
  rm -f "${pkgdir}${MINGW_PREFIX}/lib/*.def"
  rm -f "${pkgdir}${MINGW_PREFIX}/lib/gimp/2.0/modules/*.dll.a"

  cp -p "${pkgdir}${MINGW_PREFIX}/bin/gimp-${pkgver%.*}.exe" "${pkgdir}${MINGW_PREFIX}/bin/gimp.exe"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
